/**
 * Copyright (c) 2024 Hunan OpenValley Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "GPUImageRGBFilter.h"

const std::string GPUImageRGBFilter::RGB_FRAGMENT_SHADER = R"(
    #extension GL_OES_EGL_image_external : require
    varying highp vec2 textureCoordinate;

    uniform samplerExternalOES inputImageTexture;
    uniform highp float red;
    uniform highp float green;
    uniform highp float blue;

    void main()
    {
        highp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);

        gl_FragColor = vec4(textureColor.r * red, textureColor.g * green, textureColor.b * blue, 1.0);
    }
)";

GPUImageRGBFilter::GPUImageRGBFilter() : red(1.0f), green(1.0f), blue(1.0f) {}

GPUImageRGBFilter::GPUImageRGBFilter(const float redVal, const float greenVal,
                                     const float blueVal)
    : GPUImageFilter(NO_FILTER_VERTEX_SHADER, RGB_FRAGMENT_SHADER)
{
    red = redVal;
    green = greenVal;
    blue = blueVal;
}

void GPUImageRGBFilter::onInit()
{
    GPUImageFilter::onInit();
    redLocation = glGetUniformLocation(getProgram(), "red");
    greenLocation = glGetUniformLocation(getProgram(), "green");
    blueLocation = glGetUniformLocation(getProgram(), "blue");
}

void GPUImageRGBFilter::onInitialized()
{
    GPUImageFilter::onInitialized();
    setRed(red);
    setGreen(green);
    setBlue(blue);
}

void GPUImageRGBFilter::setRed(const float redVal)
{
    red = redVal;
    setFloat(redLocation, red);
}

void GPUImageRGBFilter::setGreen(const float greenVal)
{
    green = greenVal;
    setFloat(greenLocation, green);
}

void GPUImageRGBFilter::setBlue(const float blueVal)
{
    blue = blueVal;
    setFloat(blueLocation, blue);
}